import java.awt.EventQueue;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.BoxLayout;
import java.awt.Component;

public class MainWindow {
	public static HashMap<String, Client> clients; // Key: WifiIP Value: client

	private JFrame mainFrame;
	public JTable infTable;
	private JTextField fileNameTextField;
	private JFileChooser fc;

	private int req_port = 6101, data_port = 6102;
	public String fileName;
	public int fileSize_Bytes;
	public double fileSize_KB;

	private ServerRspThread rsp_thr;
	private ServerDataThread data_thr;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					clients = new HashMap<String, Client>();
					MainWindow window = new MainWindow();
					window.mainFrame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		mainFrame = new JFrame();
		mainFrame.setTitle("Server");
		mainFrame.setBounds(100, 100, 450, 300);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.getContentPane().setLayout(new BorderLayout(0, 0));

		JPanel btnPanel = new JPanel();
		mainFrame.getContentPane().add(btnPanel, BorderLayout.EAST);

		rsp_thr = new ServerRspThread(req_port, this);
		data_thr = new ServerDataThread(data_port, this);

		JButton runBtn = new JButton("Run Server");
		runBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (checkFile()) {
					new Thread(rsp_thr, "Request responsing Thread").start();
					new Thread(data_thr, "Data transmitting Thread").start();
				} else {
					JOptionPane.showMessageDialog(null, "File Path Wrong!",
							"Error", JOptionPane.ERROR_MESSAGE);

				}
			}
		});
		btnPanel.add(runBtn);

		JScrollPane scrollPanel = new JScrollPane();
		mainFrame.getContentPane().add(scrollPanel, BorderLayout.CENTER);

		Object[][] tableData = {};
		Object[] columnTitle = { "Client No.", "WiFi IP Address", "3G IP Address", "Signal Intensity" };
		infTable = new JTable(tableData, columnTitle);
		scrollPanel.setViewportView(infTable);

		JPanel panel = new JPanel();
		mainFrame.getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

		JLabel fileNameLabel = new JLabel("File Name:");
		fileNameLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
		fileNameLabel.setHorizontalAlignment(SwingConstants.LEFT);
		panel.add(fileNameLabel);

		fileNameTextField = new JTextField();
		fileNameTextField.setAlignmentY(Component.CENTER_ALIGNMENT);
		fileNameTextField.setAlignmentX(Component.CENTER_ALIGNMENT);
		fileNameTextField.setEditable(true);
		fileNameTextField.setText("");
		panel.add(fileNameTextField);
		fileNameTextField.setColumns(10);

		fc = new JFileChooser();

		JButton chooseFileBtn = new JButton("Open");
		chooseFileBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int retVal = fc.showOpenDialog(null);
				if (retVal == JFileChooser.APPROVE_OPTION) {
					fileName = fc.getSelectedFile().getAbsolutePath();
					fileNameTextField.setText(fileName);
				}
			}
		});
		chooseFileBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
		chooseFileBtn.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(chooseFileBtn);
	}

	protected boolean checkFile() {
		boolean isFile = false;
		File f;
		try {
			f = new File(fileName);
			if (f.isFile()) {
				isFile = true;
				FileInputStream fis = new FileInputStream(f);
				fileSize_Bytes = fis.available();
				fileSize_KB = Double.valueOf(fileSize_Bytes) / 1024;
				System.out.println("File Size = " + fileSize_KB + "KB");
			}
		} catch (Exception e) {
			System.out.println("File open failed");
			isFile = false;
		}
		return isFile;
	}

}
